//
//  ImagesShowTable.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/12/1.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import CooperSwift
import RxSwift

class ImagesShowTableVM: NSObject, TableViewModelProtocol, RefreshControllable, UITableViewDelegate {
    
    var currentIdx = 111
    
    func configDown() {
        toState(.loading)
        PublicMethod.delay(1) {
            self.refresh()
            self.toState(.noData)
            self.rx.refreshStatus.onNext(.endHeaderRefresh)
            self.rx.refreshStatus.onNext(.showFooter)
        }
    }
    
    func refresh() {
        forData()
        PublicMethod.delay(1) {
            self.rx.refreshStatus.onNext(.endHeaderRefresh)
        }
    }
    
    func pullUp() {
        forData()
        PublicMethod.delay(1) {
            self.rx.refreshStatus.onNext(.endFooterRefresh)
        }
    }
    
    func forData() {
        for _ in 0...30 {
            var arr = Array<TableShowImageConfig>()
            for i in 0...10 {
                arr.append(imageControl(currentIdx + i))
            }
            var imagesModel = TableShowImagesConfig.init(arr, numerOfColumn: 4)
            imagesModel.isDelay = true
            let imagsCellModel = TableShowImagesCellModel.init(imagesModel).then {
                $0.cellHeight = imagesModel.cellHeight
            }
            
            // 单个图片
            var singleCellArray = [TableShowImageCellModel]()
            for i in 0...2 {
                let singleModel = TableShowImageConfig().then {
                    let width = Screen.width
                    $0.vTopCons = 8
                    $0.vLeftCons = 8
                    $0.vBottomCons = -0.01
                    $0.masksToBounds = true
                    $0.vRadius = 8
                    $0.url = "https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/\(currentIdx + i).jpg"
                    $0.vMaxWidthCons = width - 16 - 44
                }
                singleModel.isDelay = true
                let singleImageCellModel = TableShowImageCellModel.init(singleModel).addTargetKey(bindKey())
                singleImageCellModel.cellHeight = 0.01
                singleCellArray.append(singleImageCellModel)
            }
            addFirstSectionItems(singleCellArray)
            addFirstSectionItems([imagsCellModel])
            currentIdx += 30
        }
    }
    
    func imageControl(_ number: Int) -> TableShowImageConfig {
        let model = TableShowImageConfig().then {
            let width = (Screen.width - 32.0)/3.0 - 0.01
            $0.vTopCons = 8
            $0.vLeftCons = 8
            $0.masksToBounds = true
//            $0.vRadius = 8
            $0.url = "https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/\(number).jpg"
            $0.vWidthCons = CGFloat(width)
            $0.vHeightCons = CGFloat(width)
        }
        if number == 327 {
            model.vRadius = 8
        }
        if number == 322 {
            model.cornerRadii = .init(topLeft: 8, topRight: 0, bottomLeft: 0, bottomRight: 8)
        }
        return model
    }
    
    /// 便携写法 可以直接 使用dataSource 的
    func genCellDone(item: TableCellConfigProtocol, indexPath: IndexPath, cell: UITableViewCell, table: UITableView) {
        guard let block = serviceBlock else { return }
        if let vc = block(.controller) as? LazyLoadProtocol {
            if let target = cell as? TableShowImagesCell {
                vc.addTask(indexPath) { index in
                    target.loadImagesActivity()
                    return true
                }
            }
            if let target = cell as? TableShowImageCell {
                vc.addTask(indexPath) { index in
                    target.loadImageActivity()
                    return true
                }
            }
        }
    }
    
    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return vm_tableView(tableView, heightForRowAt: indexPath)
    }
    
}

class ImagesShowTableVC: UIViewController, TableEnableProtocol, Refreshable, LazyLoadProtocol {
    
    
    let vm = ViewModelFactory.tableVM(ImagesShowTableVM.self)
    let bag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configAll(vm, disposeBag: bag, refreshConfig: .header)
        startObserve()
    }

}
